﻿#region

using Microsoft.Xna.Framework;

#endregion

namespace Pob.GameObjects
{
    /// <summary>
    ///     EIn 2D Kreis
    /// </summary>
    public class Circle
    {
        #region Fields

        /// <summary>
        ///     Radius des Kreises
        /// </summary>
        private readonly float _radius;

        /// <summary>
        ///     CMittelpunkt des Kreises
        /// </summary>
        private Vector2 _center;

        private Vector2 _direction;

        private float _distanceSquared;

        private Vector2 _v;

        #endregion

        #region Constructors and Destructors

        public Circle(Vector2 position, float radius)
        {
            _distanceSquared = 0f;
            _direction = Vector2.Zero;
            _v = Vector2.Zero;
            _center = position;
            _radius = radius;
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///     Zeigt an, ob ein Kreis ein Rechteck scneidet.
        /// </summary>
        /// <returns> Gibt True zurück, wenn sie sich schneiden, sonst False </returns>
        public bool Intersects(Rectangle rectangle)
        {
            _v = new Vector2(MathHelper.Clamp(_center.X, rectangle.Left, rectangle.Right), MathHelper.Clamp(_center.Y, rectangle.Top, rectangle.Bottom));

            _direction = _center - _v;
            _distanceSquared = _direction.LengthSquared();

            return (_distanceSquared > 0) && (_distanceSquared < _radius * _radius);
        }

        /// <summary>
        ///     Zeigt an, ob ein Kreis einen Kreis scneidet.
        /// </summary>
        /// <returns> Gibt True zurück, wenn sie sich schneiden, sonst False </returns>
        public bool Intersects(Circle circle)
        {
            if (Vector2.Distance(_center, circle._center) < (_radius + circle._radius))
            {
                return true;
            }

            return false;
        }

        #endregion
    }
}